package java_thinking.unit_21._21_2;

import net.jcip.annotations.NotThreadSafe;

/**
 * @Desc:   2-3 延迟初始化中的竟态条件(不要这么做)
 * @author: cww
 * @DateTime: 2019/5/29 20:13
 */

@NotThreadSafe
public class LazyInitRace {
    private ExpensiveObject instance = null;

    /**
     * 线程A在判断为空后新建实例过程中B线程也进来了，就发生了新建两次实例
     * @return
     */
    public ExpensiveObject getInstance() {
        if (instance == null)
            instance = new ExpensiveObject();
        return instance;
    }
}
class ExpensiveObject { }